package com.sxj.corejava.code12_线程;

/**
 * @author 石小俊
 */
public class Test07_练习 {

    public static void main(String[] args) {
        Object obj = new Object();
        HelloThread t1 = new HelloThread(obj);
        WorldThread t2 = new WorldThread(obj);
        t1.start();
        t2.start();
    }

}

class HelloThread extends Thread {
    private Object obj;

    public HelloThread(Object obj) {
        this.obj = obj;
    }

    @Override
    public void run() {
        synchronized (obj) {
            for (int i = 0; i < 10; i++) {
                System.out.print("Hello");
                // 唤醒当前线程池中的所有线程
                obj.notifyAll();
                try {
                    // 释放锁,进入等待池
                    obj.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}

class WorldThread extends Thread {
    private Object obj;

    public WorldThread(Object obj) {
        this.obj = obj;
    }

    @Override
    public void run() {
        synchronized (obj) {
            for (int i = 0; i < 10; i++) {
                System.out.print("World");
                // 唤醒当前线程池中的所有线程
                obj.notifyAll();
                try {
                    // 释放锁,进入等待池
                    obj.wait();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
            }
        }
    }
}
